package com.cdivtc.management.controller;

import com.cdivtc.management.dao.FilesurlMapper;
import com.cdivtc.management.pojo.Filesurl;
import com.cdivtc.management.pojo.Result;
import com.cdivtc.management.pojo.ResultFileUrl;
import com.cdivtc.management.service.TeacherTaskService;
import com.cdivtc.management.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;
    @Autowired
    private FilesurlMapper filesurlMapper;
    private List<Filesurl> filesurls;
    @Autowired
    private TeacherTaskService teacherTaskService;

    @RequestMapping("file")
    @ResponseBody
    public Result upload(MultipartFile file, String userId) throws IOException {
        System.out.println("userId = " + userId);
        System.out.println("file = " + file);
        Filesurl uploadfile = uploadService.uploadfile(file, userId);
        return new Result().ok().addmsg("上传成功").addData(uploadfile);
    }

    @RequestMapping("stuDoTaskUploadfile")
    @ResponseBody
    public Result stuDoTaskUploadfile(MultipartFile file) throws IOException {
        System.out.println("file = " + file);
        Filesurl uploadfile = uploadService.stuDoTaskUploadfile(file);
        return new Result().ok().addmsg("上传成功").addData(uploadfile);
    }

    //    将word  excel 生成图片
    @RequestMapping("/pdf")
    public Result pdftoimg(MultipartFile file) throws IOException {
        Date date = new Date();
        Result result = new Result();
        List<String> strings = uploadService.officeToImage(file, "");
        System.out.println("new Date().getTime()-date.getTime() = " + (new Date().getTime() - date.getTime()));
        if (strings != null && strings.size() > 0) {
            return result.ok().addData(strings);
        }
        return result.error("生成图片失败");
    }

    //   删除单个文件
    @RequestMapping("/deleteFile")
    public Result deletefile(String fileurl) {
        uploadService.deletefile(fileurl);
        return new Result().ok().addmsg("删除成功");
    }


    //   删除多个文件
    @RequestMapping("/deleteAllFile")
    public Result deletefilebeach() {
        uploadService.deletefilebeach();
        return new Result().ok().addmsg("删除成功");
    }

    @RequestMapping("/selAllFileUrlByUserId")
    public Result selAllFileUrlByUserId(String userId, String taskId) {
        System.out.println("userId = " + userId);
        System.out.println("taskId = " + taskId);
        if (userId != null && taskId != null) {
            ArrayList<ResultFileUrl> resultFileUrls = uploadService.selAllFileUrlByUserId(userId, taskId);
            System.out.println("resultFileUrls =******************************* " + resultFileUrls);
            return new Result().ok().addmsg("查询成功").addData(resultFileUrls);
        } else {
            return new Result().ok().addmsg("参数为空");
        }
    }

    @RequestMapping("/selFileUrlByTaskId")
    public Result selFileUrlByTaskId(String taskId) {
        if (taskId != null) {
            ArrayList<ResultFileUrl> resultFileUrls = uploadService.selFileUrlByTaskId(taskId);
            return new Result().ok().addmsg("查询成功").addData(resultFileUrls);
        } else {
            return new Result().ok().addmsg("参数为空");
        }
    }

    //下载文件
    @RequestMapping(value = "/testDownload", method = RequestMethod.GET)
    public void Download(HttpServletResponse res, String file) throws Exception {
        uploadService.download(res, file);
    }

    //下载文件
    @RequestMapping(value = "/Downloadbatch/{userId}/{taskId}/{username}", method = RequestMethod.GET)
    public void Downloadbatch(HttpServletResponse res, @PathVariable("userId") String userId, @PathVariable("taskId") String taskId, @PathVariable("username") String username) throws IOException {

        List<String> urls = new ArrayList<>();
        if (userId != null && taskId != null) {
            Example example1 = new Example(Filesurl.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("userId", userId);
            criteria1.andEqualTo("taskId", taskId);
            filesurls = filesurlMapper.selectByExample(example1);
            for (Filesurl filesurl : filesurls) {
                urls.add(filesurl.getUrl());
            }
        }
//        查询出负责人

        if (urls.size() > 0) {
            String downloadbatch = uploadService.downloadbatch(res, urls, username);

        }

    }

    //根据用户id删除taskid为空
    @DeleteMapping("deletefilebyUid/{uid}")
    public Result deletefilebyUid(@PathVariable("uid") String uid) {
        System.out.println("uid = " + uid);
        uploadService.deleteByTaskNull(uid);
        return new Result().ok();
    }

    @RequestMapping("delFile")
    public Result delFile(String userId) {
        System.out.println("uid = " + userId);
        teacherTaskService.delFile(userId);
        return new Result().ok();
    }


}
